package tests;

import java.util.ArrayList;
import java.util.Collection;

import simulation.AgentManager;
import simulation.Simulation;
import simulation.TrafficInfrastructureManager;
import simulation.Simulation.AgentProvider;
import agents.Agent;
import agents.resource.CrossRoadAgent;
import agents.resource.ResourceAgent;
import agents.resource.RoadAgent;
import agents.task.TaskAgent;
import domain.Car;
import domain.Clock;
import domain.Road;
import domain.Segment;
import domain.Task;
import facade.TrafficInfrastructureFacade;
import gui.MainWindow;

public class TestFlexibility implements AgentProvider {
	

	public static void main(String[] args) {
		TestFlexibility e = new TestFlexibility();
		TrafficInfrastructureManager infrastructure = e.getInfrastructure();
		
		TrafficInfrastructureFacade t = new TrafficInfrastructureFacade(infrastructure);
		
		MainWindow mainWindow = new MainWindow(t);
		mainWindow.setVisible(true);
			
		Simulation sim = new Simulation(e);
		while(true) {
			try {
				Thread.sleep(100);
				sim.doSimulationStep();
			} catch (InterruptedException i) {
				i.printStackTrace();
			}
		}
	}

	public TestFlexibility(){
		experiment = new ExperimentSetup4();
		agentManager = experiment.getAgents();
		infrastructure = experiment.getInfrastructure();
		agentManager.addAllTaskAgents(getInitialTasks());
	}
	
	public TrafficInfrastructureManager getInfrastructure(){
		return infrastructure;
	}
	
	public AgentManager getAgents(){
		return agentManager;
	}
	
	private TrafficInfrastructureManager infrastructure;
	private AgentManager agentManager;
	private ExperimentSetup experiment;
	
	private Collection<TaskAgent> getInitialTasks() {
		Collection<TaskAgent> taskAgents = new ArrayList<TaskAgent>();
		
		CrossRoadAgent c5 = (CrossRoadAgent) agentManager.getResourceAgentOf("c5");
		Car porshe = infrastructure.getCar("porshe");
		Task t1 = new Task(c5.getTrafficInfrastructure().getSegment(), porshe);
		TaskAgent porsheTask = new TaskAgent(t1);
		taskAgents.add(porsheTask);
		
		RoadAgent agent72 = (RoadAgent) agentManager.getResourceAgentOf("road72");
		Car pk = infrastructure.getCar("pk");
		Task t2 = new Task(agent72.getTrafficInfrastructure().getSegments().get(1), pk);
		TaskAgent pkTask = new TaskAgent(t2);
		taskAgents.add(pkTask);
		
		CrossRoadAgent c6 = (CrossRoadAgent) agentManager.getResourceAgentOf("c6");
		Car renault = infrastructure.getCar("renault");
		Task t3 = new Task(c6.getTrafficInfrastructure().getSegment(), renault);
		TaskAgent renaultTask = new TaskAgent(t3);
		taskAgents.add(renaultTask);
		
		CrossRoadAgent c3 = (CrossRoadAgent) agentManager.getResourceAgentOf("c3");
		Car vw = infrastructure.getCar("vw");
		Task t4 = new Task(c3.getTrafficInfrastructure().getSegment(), vw);
		TaskAgent vwTask = new TaskAgent(t4);
		taskAgents.add(vwTask);
		
		Car volvo = infrastructure.getCar("volvo");
		Task t5 = new Task(c5.getTrafficInfrastructure().getSegment(), volvo);
		TaskAgent volvoTask = new TaskAgent(t5);
		taskAgents.add(volvoTask);
		
		CrossRoadAgent c1 = (CrossRoadAgent) agentManager.getResourceAgentOf("c1");
		
		Car suzuki = infrastructure.getCar("suzuki");
		Task t7 = new Task(c1.getTrafficInfrastructure().getSegment(), suzuki);
		TaskAgent suzukiTask = new TaskAgent(t7);
		taskAgents.add(suzukiTask);
		
		Car ferrari = infrastructure.getCar("ferrari");
		Task t8 = new Task(c1.getTrafficInfrastructure().getSegment(), ferrari);
		TaskAgent ferrariTask = new TaskAgent(t8);
		taskAgents.add(ferrariTask);
		
		
		Car opel = infrastructure.getCar("opel");
		Task t9 = new Task(agent72.getTrafficInfrastructure().getSegments().get(5), opel);
		TaskAgent opelTask = new TaskAgent(t9);
		taskAgents.add(opelTask);
		
		//RoadAgent agent82 = (RoadAgent) agentManager.getResourceAgentOf("road82");
		Car ford = infrastructure.getCar("ford");
		Task t10 = new Task(agent72.getTrafficInfrastructure().getSegments().get(1), ford);
		TaskAgent fordTask = new TaskAgent(t10);
		taskAgents.add(fordTask);
		
		Car spyker = infrastructure.getCar("spyker");
		Task t11 = new Task(agent72.getTrafficInfrastructure().getSegments().get(4), spyker);
		TaskAgent spykerTask = new TaskAgent(t11);
		taskAgents.add(spykerTask);
		
		Road road13 = (Road) getAgents().getResourceAgentOf("road13").getTrafficInfrastructure();
		//RoadAgent agent67 = (RoadAgent) agents.getResourceAgentOf("road67");
		Car mclaren = infrastructure.getCar("mclaren");
		Task t12 = new Task(road13.getSegments().get(2), mclaren);
		TaskAgent mclarenTask = new TaskAgent(t12);
		taskAgents.add(mclarenTask);
		
		Car toyota = infrastructure.getCar("toyota");
		Task t13 = new Task(c1.getTrafficInfrastructure().getSegment(), toyota);
		TaskAgent toyTask = new TaskAgent(t13);
		taskAgents.add(toyTask);
		
		return taskAgents;
	}

	public Collection<Agent> getNewAgents() {
		Collection<Agent> newAgents = new ArrayList<Agent>();
		if(Clock.getInstance().getCurrentTime() == 0) {
			newAgents.addAll(agentManager.getAllAgents());
		
		} else {
			for(TaskAgent ta : agentManager.getAllTaskAgents()) {
				if(ta.isCompleted()) {
					Car car = ta.getCar();
					Segment startPos = experiment.getStartPosition(car);
					if(startPos.getCar() == null) {
						agentManager.removeTaskAgent(ta);
						TaskAgent newTask = new TaskAgent(ta.getTask());
						car.move(startPos);
						newAgents.add(newTask);
						agentManager.addTaskAgent(newTask);
					}
				}
			}
		}
		return newAgents;
	}

	public Collection<Agent> getAgentsToRemove() {
		ArrayList<Agent> agentsToDie = new ArrayList<Agent>();
		ResourceAgent mrBond = agentManager.getResourceAgentOf("road78");
		if(Clock.getInstance().getCurrentTime() >= 50 && !mrBond.isRemoved() && !mrBond.hasCars()) {
			agentsToDie.add(mrBond);
		}
		ResourceAgent flik = agentManager.getResourceAgentOf("c8");
		if(Clock.getInstance().getCurrentTime() >= 150 && !flik.isRemoved() && !flik.hasCars()) {
			agentsToDie.add(flik);
		}
		return agentsToDie;
	}

}
